import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/login', name: 'Login', component: Login },
  { path: '/home', name: 'Home', component: Home }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 添加路由守卫，进行请求的拦截
// router.beforeEach((to, from, next) => {
//   // to 将要访问的路径
//   // from 从哪个路径来
//   // next 放行函数
//   //      next()          放行
//   //      next('/login')  强制跳转
//   if (to.path === '/login') return next()
//   // 当访问其他路径，且没有登录时，跳转到登录界面   ==> 通过是不是有token进行判断
//   const token = window.sessionStorage.getItem('token')
//   if (!token) return next('/login')
//   next()
// })

export default router
